package com.wyh.service.serviceImpl.gfiveImpl;


import com.wyh.common.enums.ResultCodeEnum;
import com.wyh.common.exception.CustomException;
import com.wyh.domain.dto.gfive.InvoiceInquireDTO;
import com.wyh.domain.dto.gfive.InvoicedetailsDTO;
import com.wyh.domain.dto.gfive.ReceiptConfigurationDTO;
import com.wyh.domain.po.gfive.Invoicedetails;
import com.wyh.domain.po.gfive.Invoicerecords;
import com.wyh.mapper.gfivemapper.ReceiptConfigurationMapper;
import com.wyh.service.gfive.ReceiptConfigurationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.List;


@Service
public class ReceiptConfigurationServiceImpl implements ReceiptConfigurationService {

    @Autowired
    private ReceiptConfigurationMapper receiptConfigurationMapper;

    /*
     * 分页，条件查询收据记录
     * */
    @Override
    @Transactional
    public List<ReceiptConfigurationDTO> findAll(String noticenumber, String customername, BigDecimal minReceivable, BigDecimal maxReceivable, int page, int size) {
        int offset = (page - 1) * size;
        List<ReceiptConfigurationDTO> all = receiptConfigurationMapper.findAll(noticenumber, customername, minReceivable, maxReceivable, offset, size);


        for (ReceiptConfigurationDTO dto : all) {
            if (dto != null) {
                int uploadedRecordCount = receiptConfigurationMapper.count(dto.getId());
                dto.setUploadedRecordCount(uploadedRecordCount);

                BigDecimal uninvoicedAmount = (dto.getReceivable() != null && dto.getAmount() != null)
                        ? dto.getReceivable().subtract(dto.getAmount())
                        : BigDecimal.ZERO;
                dto.setUninvoicedAmount(uninvoicedAmount);
            }
        }

        return all;
    }

    @Override
    public Long countAll(String noticenumber, String customername, BigDecimal minReceivable, BigDecimal maxReceivable) {
        return receiptConfigurationMapper.countAll(noticenumber, customername, minReceivable, maxReceivable);
    }

    /*
     * 上传发票业务逻辑
     * */

    @Override
    @Transactional
    public void upReceipts(InvoicedetailsDTO invoicedetails) {
        // 判断金额
        InvoiceInquireDTO map = receiptConfigurationMapper.findBillByNumber(invoicedetails.getNoticenumber());

        if (map == null) {
            throw new CustomException(ResultCodeEnum.PARAM_ERROR);
        }

        Double receivable = map.getReceivable();
        Double amount = map.getAmount();
        int id = map.getId();

        if (receivable == null || amount == null) {
            throw new CustomException(ResultCodeEnum.PARAM_ERROR);
        }

        double i = invoicedetails.getAmount() + amount - receivable;
        if (i > 0) {
            throw new CustomException(ResultCodeEnum.EXCESS_AMOUNT);
        }

        // 更改发票记录表信息
        Invoicerecords i2 = new Invoicerecords();
        i2.setAmount(invoicedetails.getAmount() + amount);
        i2.setId(id);

        receiptConfigurationMapper.upByInvoicerecords(i2);

        // 新增开票明细表信息
        invoicedetails.setInvoiceId(id);
        receiptConfigurationMapper.upReceipts(invoicedetails);
    }

    /*
     * 预览收据业务
     * */
    @Override
    public List<Invoicedetails> findInvoicedetails(String noticenumber) {
        if (noticenumber == null) {
            throw new CustomException(ResultCodeEnum.PARAM_ERROR);
        }


        return receiptConfigurationMapper.findInvoicedetails(noticenumber);
    }

}
